package com.jeomo.mem.provider.web;

import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.mem.provider.request.*;
import com.jeomo.mem.provider.vo.MemberDiscountRuleVo;
import com.jeomo.mem.starter.dto.MemberDiscountRuleDto;
import com.jeomo.mem.starter.entity.MemberDiscountRule;
import com.jeomo.mem.starter.entity.MemberDiscountRuleList;
import com.jeomo.mem.starter.service.IMemberDiscountRuleService;
import com.jeomo.mem.starter.service.IMemberDiscountService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员折扣规则
 * @author:Jackpot
 * @create 2020-09-09 16:24
 * @description:
 */
@RestController
@RequestMapping("/member/discount")
public class MemberDiscountRuleController {

    @Autowired
    private IMemberDiscountService memberDiscountService;

    @Resource
    private IMemberDiscountRuleService memberDiscountRuleService;

    @ApiOperation("会员折扣规则定义接口")
    @PostMapping("/rule")
    public CommonResult saveRule(@RequestBody @ApiParam("会员折扣规则") @Validated MemberDiscountRequest memberDiscountRequest){

        MemberDiscountRuleRequest memberDiscountRuleRequest = memberDiscountRequest.getMemberDiscountRule();
        List<MemberDiscountListRequest> memberDiscountListRequest = memberDiscountRequest.getMemberDiscountRuleList();

        MemberDiscountRule memberPointsRule = BeanCopyUtil.copyProperties(memberDiscountRuleRequest,MemberDiscountRule.class);
        List<MemberDiscountRuleList> memberPointsRuleList =
                BeanCopyUtil.copyListProperties(memberDiscountListRequest, MemberDiscountRuleList::new);

        int resultCode = memberDiscountService.saveRule(memberPointsRule, memberPointsRuleList);

        if (resultCode >0){
            return CommonResult.success("保存成功");
        }else if (resultCode == -1){
            return CommonResult.failure("数据已发生变化,请刷新后重试！");
        }else if (resultCode == -2){
            return CommonResult.failure("数据已被删除，操作失败！");
        }else if (resultCode == -3){
            return CommonResult.failure("配置项不能重复！");
        }else{
            return CommonResult.failure("保存失败");
        }
    }

    @ApiOperation("获取一条积分规则的详情信息")
    @GetMapping("/rule/{ruleCode}")
    public CommonResult getDiscountRuleById(@PathVariable String ruleCode){
        MemberDiscountRuleDto memberRule = memberDiscountService.getMemberRuleById(ruleCode);
        return CommonResult.success(memberRule);
    }

    @ApiOperation("分页获取会员折扣规则")
    @PostMapping("/rules/page")
    public CommonResult<PageResponseVo<MemberDiscountRuleVo>> getRulesByPageCondition(@RequestBody PageRequest pageRequest){

        PageQueryDto dto = BeanCopyUtil.copyProperties(pageRequest, PageQueryDto.class);
        PageResponseDto pageResponseDto = memberDiscountRuleService.pageSearch(dto);
        PageResponseVo<MemberDiscountRuleVo> memPointsRulePageResVo = BeanCopyUtil.copyPageProperties(pageResponseDto, MemberDiscountRuleVo.class);
        return CommonResult.success(memPointsRulePageResVo);
    }


    @ApiOperation("计算折扣")
    @GetMapping("/point/{levelCode}/{goodsCode}")
    public CommonResult getDiscount(@PathVariable String levelCode,@PathVariable String goodsCode){
        double points = memberDiscountService.getDiscount(levelCode, goodsCode);
        return CommonResult.success(points);
    }

    @ApiOperation("删除单条会员折扣规则")
    @DeleteMapping("/rule/{ruleCode}")
    public CommonResult deleteRule(@PathVariable String ruleCode){
        int i = memberDiscountService.deleteRule(ruleCode);
        return i>0?CommonResult.success("删除成功"):CommonResult.failure("操作失败");
    }

    @ApiOperation("停用/启用会员折扣规则")
    @PostMapping("/rule/status")
    public CommonResult updateRuleStatus(@RequestBody MemStatusRequest memStatusRequest){
        String ruleCode = memStatusRequest.getRuleCode();
        Boolean status = memStatusRequest.getStatus();
        int i = memberDiscountRuleService.updateRuleStatus(ruleCode, status);
        return i>0?CommonResult.success("操作成功"):CommonResult.failure("操作失败");
    }

}
